import Cesium from '../../Ces/Cesium'
/**
 * 飞行尾迹
 */
function FlyingTail(options) {
  const positions = options.positions;

  this.createLine(positions);
  this.createWall(positions);
  this._showPercent = 0.5;
  this._showLength = 0.1;
}

Object.defineProperties(FlyingTail.prototype, {
  showPercent: {
    get() {
      return this._showPercent;
    },
    set(value) {
      this._showPercent = value;
    },
  },
  showLength: {
    get() {
      return this._showLength;
    },
    set(value) {
      this._showLength = value;
    },
  },
});

FlyingTail.prototype.createLine = function (positions) {
  const appearance = new Cesium.PolylineMaterialAppearance({
    renderState: {
      depthTest: { enabled: true },
      depthMask: true,
      blending: Cesium.BlendingState.ALPHA_BLEND,
      cull: {
        enabled: false,
        face: Cesium.CullFace.FRONT,
      },
    },
    translucent: true,
  });

  const fs = `
    precision highp float;
    czm_material czm_getMaterial(czm_materialInput materialInput) {
            czm_material material = czm_getDefaultMaterial(materialInput);
            vec2 st = materialInput.st;
            float glow = glowPower / abs(st.t - 0.5) - (glowPower / 0.5);

            if (taperPower <= 0.99999) {
                glow *= min(1.0, taperPower / (0.5 - st.s * 0.5) - (taperPower / 0.5));
            }

            vec4 fragColor;
            fragColor.rgb = max(vec3(glow - 1.0 + color.rgb), color.rgb);
            fragColor.a = clamp(0.0, 1.0, glow) * color.a;
            fragColor = czm_gammaCorrect(fragColor);

            material.emission = fragColor.rgb;
            material.alpha = fragColor.a;
            // vec2 v = gl_FragCoord.xy / czm_viewport.zw;
            if(st.x > showPercent * 0.01 || st.x < showPercent * 0.01 - showLength) discard;

            return material;
        }
        `;

  appearance.material = new Cesium.Material({
    fabric: {
      source: fs,
      uniforms: {
        color: new Cesium.Color(0.0, 0.5, 1.0, 1.0),
        glowPower: 0.25,
        taperPower: 1.0,
        showPercent: 1.0,
        showLength: 1.0,
      },
    },
  });

  const that = this;

  appearance.material._uniforms.showPercent_3 = function () {
    return that._showPercent;
  };
  appearance.material._uniforms.showLength_4 = function () {
    return that._showLength;
  };

  const geometryInstances = new Cesium.GeometryInstance({
    geometry: new Cesium.PolylineGeometry({
      positions,
      width: 4,
    }),
  });

  this._linePrimitive = new Cesium.Primitive({
    geometryInstances,
    asynchronous: false,
    appearance,
  });
};

FlyingTail.prototype.createWall = function (positions) {
  const geometry = Cesium.WallGeometry.createGeometry(
    new Cesium.WallGeometry({
      positions,
    }),
  );

  const appearance = new Cesium.MaterialAppearance({
    renderState: {
      depthTest: { enabled: true },
      depthMask: true,
      blending: Cesium.BlendingState.ALPHA_BLEND,
      cull: {
        enabled: false,
        face: Cesium.CullFace.FRONT,
      },
    },
    translucent: true,
  });

  const fs = `
    precision highp float;
    czm_material czm_getMaterial(czm_materialInput materialInput) {
            czm_material material = czm_getDefaultMaterial(materialInput);
            vec2 st = materialInput.st;
            vec4 color = vec4(0.5, 0.5, st.y, 1.0);
            material.diffuse = color.rgb;
            material.alpha = pow(st.y,1.1);
            float f = (st.x * 0.5) + 0.5;
            // material.alpha *= pow(f,2.0);
            if(st.x > showPercent * 0.01 || st.x < showPercent - showLength) discard;
            return material;
        }
        `;

  appearance.material = new Cesium.Material({
    fabric: {
      source: fs,
      uniforms: {
        showPercent: 1.0,
        showLength: 1.0,
      },
    },
  });

  const that = this;

  appearance.material._uniforms.showPercent_0 = function () {
    return that._showPercent;
  };
  appearance.material._uniforms.showLength_1 = function () {
    return that._showLength;
  };

  this._wallPrimitive = new Cesium.Primitive({
    geometryInstances: new Cesium.GeometryInstance({ geometry }),
    asynchronous: false,
    appearance,
  });
};


FlyingTail.prototype.destroy = function () {
  this._linePrimitive = this._linePrimitive && this._linePrimitive.destroy();
  this._wallPrimitive = this._wallPrimitive && this._wallPrimitive.destroy();

  return Cesium.destroyObject(this);
};

FlyingTail.prototype.isDestroyed = function () {
  return false;
};

FlyingTail.prototype.update = function (frameState) {
  if (frameState.passes.render) {
    this._wallPrimitive.update(frameState);
    this._linePrimitive.update(frameState);
  }
};

export default FlyingTail;
